package com.github.xzb617.sample;

import com.alibaba.fastjson.JSON;
import com.github.xzb617.annotation.TreeNodeLevel;
import com.github.xzb617.exception.NoSuchTreeNodeException;
import com.github.xzb617.exception.TreeNodeCannotSetValueException;
import com.github.xzb617.func.FirstLevelNodeConsumer;
import com.github.xzb617.helper.TreeHelper;
import com.github.xzb617.utils.ReflectUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xzb617
 * @date 2022/5/16 9:42
 * @description:
 */
public class TreeSample {

    private static List<TreeModel> MOCK_TREE_DATA =  null;

    static {
        MOCK_TREE_DATA = new ArrayList<>();
        MOCK_TREE_DATA.add(new TreeModel(1L, "node1", 0L, 1, 1));
        MOCK_TREE_DATA.add(new TreeModel(11L, "node11", 1L, 2,1));
        MOCK_TREE_DATA.add(new TreeModel(12L, "node12", 1L, 2,2));
        MOCK_TREE_DATA.add(new TreeModel(111L, "node111", 11L, 3,1));
        MOCK_TREE_DATA.add(new TreeModel(112L, "node112", 11L, 3,2));
        MOCK_TREE_DATA.add(new TreeModel(121L, "node121", 12L, 3,2));
        MOCK_TREE_DATA.add(new TreeModel(122L, "node122", 12L, 3,1));

        MOCK_TREE_DATA.add(new TreeModel(2L, "node2", 0L, 1,2));
        MOCK_TREE_DATA.add(new TreeModel(3L, "node3", 0L, 1,3));

        MOCK_TREE_DATA.add(new TreeModel(3001L, "node3001", -1L, 2));
        MOCK_TREE_DATA.add(new TreeModel(3002L, "node3002", -2L, 2));
    }

    /**
     * 注解方式
     * @return
     */
    public static List<TreeModel> testCreateTree01() {
        return TreeHelper.createTree(MOCK_TREE_DATA, true, true);
    }

    /**
     * 字段名方式
     * @return
     */
    public static List<TreeModel> testCreateTree02() {
        return TreeHelper.createTree(MOCK_TREE_DATA, "id", "parentId", "children",true, "order", true);
    }

    /**
     * 注解方式，且自定义第一级数据判定逻辑
     * @return
     */
    public static List<TreeModel> testCreateTree03() {
        return TreeHelper.createTree(MOCK_TREE_DATA, true, true, new FirstLevelNodeConsumer() {
            @Override
            public <T> boolean isFirstLevelNode(List<T> nodes, T childNode) {
                Object level = null;
                try {
                    level = ReflectUtil.getFieldValue(TreeNodeLevel.class, childNode);
                } catch (IllegalAccessException | IllegalArgumentException e) {
                    throw new TreeNodeCannotSetValueException(e.getMessage(), e);
                } catch (NoSuchFieldException e) {
                    throw new NoSuchTreeNodeException(e.getMessage(), e);
                }
                return level!=null && (int) level == 1;
            }
        });
    }

    /**
     * 注解方式，且自定义第一级判定逻辑，且将未找到父节点的节点归类到自定义节点下
     * @return
     */
    public static List<TreeModel> testCreateTree04() {
        return TreeHelper.createTree(MOCK_TREE_DATA, true, true,new FirstLevelNodeConsumer() {
            @Override
            public <T> boolean isFirstLevelNode(List<T> nodes, T childNode) {
                Object fieldValue = null;
                try {
                    fieldValue = ReflectUtil.getFieldValue(TreeNodeLevel.class, childNode);
                } catch (IllegalAccessException | IllegalArgumentException e) {
                    throw new TreeNodeCannotSetValueException(e.getMessage(), e);
                } catch (NoSuchFieldException e) {
                    throw new NoSuchTreeNodeException(e.getMessage(), e);
                }
                return fieldValue != null && Integer.parseInt(String.valueOf(fieldValue)) == 1;
            }
        }, new TreeModel(300L, "mnode", 1L, 1, 4));
    }

    public static void main(String[] args) {
        System.out.println(JSON.toJSONString(testCreateTree01()));
        System.out.println(JSON.toJSONString(testCreateTree02()));
        System.out.println(JSON.toJSONString(testCreateTree03()));
        System.out.println(JSON.toJSONString(testCreateTree04()));
    }
}
